package com.snopy.thread.countdownlatch;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @author snopy
 * @version 1.0.0
 * @classname DemoCountDownLatch
 * @description
 * @email 77912204@qq.com
 * @date 2022/11/19 14:11
 */
public class DemoCountDownLatch {

    static int num = 0;
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch count = new CountDownLatch(3);
        Thread t1 = new Thread(() -> {
            try {
                num++;
                TimeUnit.SECONDS.sleep(4);
                count.countDown();
                System.out.println("task1------");
                TimeUnit.SECONDS.sleep(1);
                count.countDown();
                System.out.println("task2------");
                System.out.println(count.getCount());
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }, "t1");
        t1.start();
        Thread t2 = new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(5);
                System.out.println(count.getCount());
                num = num + 10;
                count.countDown();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "t2");
        t2.start();
        Thread t3 = new Thread(() -> {
            try {
                System.out.println(count.getCount());
                count.await(6,TimeUnit.SECONDS);
                System.out.println("num:" + num);
                System.out.println(count.getCount());
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "t3");
        t3.start();

        //System.out.println("num: "+num);
    }
}
